Does allocating cooperative individuals to high-degree nodes prevent isolation?

round = NULL
gameID = NULL 
nodes = NULL
meanDegree = NULL
meanDegreeCoop = NULL #mean degree among those who cooperated
meanDegreeDefect = NULL #mean degree among those who defected
meanDist = NULL #mean shortest path length divided by the maximum possible path length (Nvertices - 1)
maxDist = NULL #max shortest path length divided by the maximum possible path length (Nvertices - 1)
coefGlobal = NULL #global clustering coefficient
percentCoop = NULL #proportion of those who cooperated
percentCoopPopular = NULL #proportion of those who cooperated, among those with degree >=10
coopEnv = NULL 
assort = NULL

for(i in 1:length(g.isolation)){
    round[i] =  mean(V(g.isolation[[i]])$round)
    gameID[i] = V(g.isolation[[i]])$gameID[1]
    nodes[i] = vcount(g.isolation[[i]])
    meanDegree[i] = mean(igraph::degree(g.isolation[[i]]),na.rm=TRUE)
    meanDegreeCoop[i] = mean(igraph::degree(g.isolation[[i]])[V(g.isolation[[i]])$behavior=="C"],na.rm=TRUE)
    meanDegreeDefect[i] = mean(igraph::degree(g.isolation[[i]])[V(g.isolation[[i]])$behavior=="D"],na.rm=TRUE)
    meanDist[i] = mean(distances(g.isolation[[i]])[distances(g.isolation[[i]])!=0 & distances(g.isolation[[i]])!=Inf],na.rm=TRUE) / length(V(g.isolation[[i]])-1)
    maxDist[i] = max(distances(g.isolation[[i]])[distances(g.isolation[[i]])!=0 & distances(g.isolation[[i]])!=Inf],na.rm=TRUE) / length(V(g.isolation[[i]])-1)
    coefGlobal[i] = transitivity(g.isolation[[i]], type="global")
    percentCoop[i] = prop.table(table(V(g.isolation[[i]])$behavior))["C"]
    percentCoopPopular[i] = prop.table(table(V(g.isolation[[i]])$behavior[igraph::degree(g.isolation[[i]])>=6]))["C"]
    coopEnv[i] = sum(ifelse(V(g.isolation[[i]])$behavior=="C",1,0)*igraph::degree(g.isolation[[i]])/sum(igraph::degree(g.isolation[[i]])),na.rm=TRUE)
    assort[i] = assortativity(g.isolation[[i]], V(g.isolation[[i]])$behavior == "C")
}

smallWorld = data.frame(
  round = round,
  gameID = gameID,
  nodes = nodes,
  meanDegree = meanDegree,
  meanDegreeCoop = meanDegreeCoop,
  meanDegreeDefect = meanDegreeDefect,
  diffDegreeCD = meanDegreeCoop - meanDegreeDefect,
  meanDist = meanDist,
  maxDist = maxDist,
  coefGlobal = coefGlobal,
  percentCoop = percentCoop,
  percentCoopPopular = percentCoopPopular,
  coopEnv = coopEnv,
  assort = assort
)


round = NULL
gameID = NULL
visibleWealth = NULL
n=1
for(i in unique(cdata$round)){
  for(m in unique(cdata$gameID)){
    round[n] = as.numeric(i)-1
    gameID[n] = m
    visibleWealth[n] = ifelse(cdata[cdata$round==i & cdata$gameID==m,]$showScore[1]=="true",1,0)
    n=n+1
  }
}

smallWorld.2 = data.frame(
  round = round,
  gameID = gameID,
  visibleWealth = visibleWealth
)

smallWorld = merge(smallWorld, smallWorld.2, by=c("gameID","round"), all.x=TRUE)

smallWorld.initial = subset(smallWorld, smallWorld$round==0)

#percent of people that end up being isolated for each gameID
percentIsolated = NULL
percentIsolated = aggregate(isolated ~ gameID, data=sample.wide, FUN=max, na.rm=TRUE)
smallWorld.initial = merge(smallWorld.initial[c("gameID","meanDegree","meanDegreeCoop","meanDegreeDefect","diffDegreeCD","meanDist","coefGlobal","visibleWealth","percentCoop","percentCoopPopular","coopEnv","assort")],percentIsolated, by="gameID", all.x=TRUE)

percentIsolated = NULL
percentIsolated = aggregate(isolated ~ gameID, data=sample.wide, FUN=mean, na.rm=TRUE)
percentIsolated = percentIsolated %>% rename(
  percentIsolated = isolated
)
smallWorld.initial = merge(smallWorld.initial,percentIsolated, by="gameID", all.x=TRUE)




reg = glm(isolated ~ coopEnv, 
      data = smallWorld.initial,
      family = binomial(link = "logit"))
summary(reg)
## 
## Call:
## glm(formula = isolated ~ coopEnv, family = binomial(link = "logit"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.3756   0.2641   0.4839   0.6926   1.3696  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)    6.047      1.734   3.487 0.000489 ***
## coopEnv       -6.813      2.351  -2.898 0.003750 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 82.760  on 79  degrees of freedom
## Residual deviance: 72.371  on 78  degrees of freedom
## AIC: 76.371
## 
## Number of Fisher Scoring iterations: 5
reg = glm(isolated ~ coopEnv*percentCoop, 
      data = smallWorld.initial,
      family = binomial(link = "logit"))
summary(reg)
## 
## Call:
## glm(formula = isolated ~ coopEnv * percentCoop, family = binomial(link = "logit"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.2132   0.4220   0.4658   0.5862   1.7551  
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)
## (Intercept)           -3.096      6.866  -0.451    0.652
## coopEnv               10.481     12.639   0.829    0.407
## percentCoop           10.681     12.929   0.826    0.409
## coopEnv:percentCoop  -20.525     14.822  -1.385    0.166
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 82.760  on 79  degrees of freedom
## Residual deviance: 70.472  on 76  degrees of freedom
## AIC: 78.472
## 
## Number of Fisher Scoring iterations: 4
reg = glm(isolated ~ assort, 
      data = smallWorld.initial,
      family = binomial(link = "logit"))
summary(reg)
## 
## Call:
## glm(formula = isolated ~ assort, family = binomial(link = "logit"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.9579   0.5132   0.6439   0.7269   1.0178  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   1.1349     0.3207   3.539 0.000402 ***
## assort       -3.7358     3.0781  -1.214 0.224862    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 67.731  on 64  degrees of freedom
## Residual deviance: 66.177  on 63  degrees of freedom
##   (15 observations deleted due to missingness)
## AIC: 70.177
## 
## Number of Fisher Scoring iterations: 4
reg = glm(isolated ~ assort*percentCoop, 
      data = smallWorld.initial,
      family = binomial(link = "logit"))
summary(reg)
## 
## Call:
## glm(formula = isolated ~ assort * percentCoop, family = binomial(link = "logit"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.5706   0.1873   0.4272   0.6978   1.3591  
## 
## Coefficients:
##                    Estimate Std. Error z value Pr(>|z|)   
## (Intercept)           6.695      2.392   2.799  0.00512 **
## assort              -23.388     22.419  -1.043  0.29684   
## percentCoop          -7.680      3.186  -2.411  0.01593 * 
## assort:percentCoop   25.842     29.451   0.877  0.38024   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 67.731  on 64  degrees of freedom
## Residual deviance: 56.386  on 61  degrees of freedom
##   (15 observations deleted due to missingness)
## AIC: 64.386
## 
## Number of Fisher Scoring iterations: 5
reg = glm(percentIsolated ~ coopEnv, 
      data = smallWorld.initial, 
      family = gaussian(link = "identity"))
summary(reg)
## 
## Call:
## glm(formula = percentIsolated ~ coopEnv, family = gaussian(link = "identity"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.12622  -0.05564  -0.02541   0.03834   0.25903  
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.20852    0.04303   4.846 6.29e-06 ***
## coopEnv     -0.17058    0.06391  -2.669  0.00925 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.006957149)
## 
##     Null deviance: 0.59222  on 79  degrees of freedom
## Residual deviance: 0.54266  on 78  degrees of freedom
## AIC: -166.43
## 
## Number of Fisher Scoring iterations: 2
reg = glm(percentIsolated ~ coopEnv*percentCoop, 
      data = smallWorld.initial, 
      family = gaussian(link = "identity"))
summary(reg)
## 
## Call:
## glm(formula = percentIsolated ~ coopEnv * percentCoop, family = gaussian(link = "identity"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.15277  -0.04970  -0.01807   0.03678   0.25172  
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)
## (Intercept)           0.1084     0.1925   0.563    0.575
## coopEnv               0.4950     0.3742   1.323    0.190
## percentCoop          -0.2223     0.3692  -0.602    0.549
## coopEnv:percentCoop  -0.3977     0.4417  -0.900    0.371
## 
## (Dispersion parameter for gaussian family taken to be 0.006743275)
## 
##     Null deviance: 0.59222  on 79  degrees of freedom
## Residual deviance: 0.51249  on 76  degrees of freedom
## AIC: -167.01
## 
## Number of Fisher Scoring iterations: 2
reg = glm(percentIsolated ~ assort, 
      data = smallWorld.initial, 
      family = gaussian(link = "identity"))
summary(reg)
## 
## Call:
## glm(formula = percentIsolated ~ assort, family = gaussian(link = "identity"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.13800  -0.04889  -0.01599   0.03676   0.29844  
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.08136    0.01164   6.988  2.1e-09 ***
## assort      -0.33980    0.09891  -3.435  0.00105 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.006992089)
## 
##     Null deviance: 0.52302  on 64  degrees of freedom
## Residual deviance: 0.44050  on 63  degrees of freedom
##   (15 observations deleted due to missingness)
## AIC: -134.16
## 
## Number of Fisher Scoring iterations: 2
reg = glm(percentIsolated ~ assort*percentCoop, 
      data = smallWorld.initial, 
      family = gaussian(link = "identity"))
summary(reg)
## 
## Call:
## glm(formula = percentIsolated ~ assort * percentCoop, family = gaussian(link = "identity"), 
##     data = smallWorld.initial)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.13441  -0.04183  -0.01733   0.02556   0.27510  
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         0.22289    0.05970   3.733 0.000418 ***
## assort             -0.70774    0.42639  -1.660 0.102080    
## percentCoop        -0.20362    0.08504  -2.394 0.019731 *  
## assort:percentCoop  0.59881    0.63264   0.947 0.347615    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.006108948)
## 
##     Null deviance: 0.52302  on 64  degrees of freedom
## Residual deviance: 0.37265  on 61  degrees of freedom
##   (15 observations deleted due to missingness)
## AIC: -141.04
## 
## Number of Fisher Scoring iterations: 2
# #MSM
# # estimation of denominator of ip weights
# #cooperatorの数以外は全てRandom(Erdos-Renyi random graphにより決まるため)なので、coopEnvはpercentCoopで調整すれば十分か
# den.fit.obj <- lm(coopEnv ~ percentCoop + I(percentCoop^2), data = smallWorld.initial)
# p.den <- predict(den.fit.obj, type = "response")
# dens.den <- dnorm(smallWorld.initial$coopEnv, p.den, summary(den.fit.obj)$sigma)
# 
# # estimation of numerator of ip weights
# num.fit.obj <- lm(coopEnv ~ 1, data = smallWorld.initial)
# p.num <- predict(num.fit.obj, type = "response")
# dens.num <- dnorm(smallWorld.initial$coopEnv, p.num, summary(num.fit.obj)$sigma)
# 
# smallWorld.initial$sw.a = dens.num/dens.den
# summary(smallWorld.initial$sw.a)
# 
# msm.sw.cont <-geepack::geeglm(isolated ~ coopEnv, 
#                  data=smallWorld.initial, weights=sw.a, family = binomial(link="logit"), id=gameID, corstr="independence")
# summary(msm.sw.cont)
# 
# msm.sw.cont <-geepack::geeglm(percentIsolated ~ coopEnv, 
#                  data=smallWorld.initial, weights=sw.a, family = gaussian(link="identity"), id=gameID, corstr="independence")
# summary(msm.sw.cont)


ggplot(data = smallWorld.initial, aes(x = coopEnv, y = percentIsolated)) + 
  geom_point() +
  ggtitle("Percent isolated") +
  scale_x_continuous("Cooporative environment") +  
  scale_y_continuous("Percent of individuals isolated") + 
  scale_fill_continuous(type = "viridis")

ggplot(data = smallWorld.initial, aes(x = assort, y = percentIsolated)) + 
  geom_point() +
  ggtitle("Percent isolated") +
  scale_x_continuous("Assortativity coefficient of initial C/D") +  
  scale_y_continuous("Percent of individuals isolated") + 
  scale_fill_continuous(type = "viridis")
## Warning: Removed 15 rows containing missing values (geom_point).

Simulation 1

All graphs start as Erdos-Renyi random newtorks

Change where to place the cooperators in the network: 1. defectors have high degrees

#Define degrees of isolation
isolationDegree = 2

#number of iterations per arm
iterations = 500


df.netIntLowDegree = data.frame(
  coopFrac = NULL,
  avgCoop = NULL, 
  avgCoopFinal = NULL, 
  percentIsolation = NULL,
  isolation = NULL,
  percentIsolatedD = NULL,
  nCommunities = NULL,
  communitySize = NULL,
  assortativityInitial = NULL,
  assortativityFinal = NULL,
  conversionRate = NULL,
  conversionToD = NULL, 
  conversionToC = NULL, 
  degreeC = NULL, 
  degreeD = NULL,
  meanConversionToD = NULL, 
  meanConversionToC = NULL, 
  degreeLost = NULL,
  degreeLostC = NULL,
  degreeLostD = NULL
)


for(frac in c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1)){
  #nodes in the top fractionCoop degrees will automatically be a cooperator
  fractionCoop = frac
  
    coopFrac = NULL
    avgCoop = NULL
    avgCoopFinal = NULL
    percentIsolation = NULL
    isolation = NULL
    percentIsolatedD = NULL
    nCommunities = NULL
    communitySize = NULL
    assortativityInitial = NULL
    assortativityFinal = NULL
    conversionRate = NULL
    conversionToD = NULL 
    conversionToC = NULL
    degreeC = NULL
    degreeD = NULL
    meanConversionToD = NULL 
    meanConversionToC = NULL
    degreeLost = NULL
    degreeLostC = NULL
    degreeLostD = NULL
    for(m in c(1:iterations)){
      # Section 1. NOTES, packages, and Parameters
      #Importing library
      library(igraph) # for network graphing
      library(reldist) # for gini calculatio
      library(boot) # for inv.logit calculation
      #Two prefixed functions
      #rank
      rank1 = function(x) {rank(x,na.last=NA,ties.method="average")[1]} #a smaller value has a smaller rank.
      #gini mean difference (a.k.a. mean difference: please refer to https://stat.ethz.ch/pipermail/r-help/2003-April/032782.html)
      gmd = function(x) {
       x1 = na.omit(x)
       n = length(x1)
      tmp = 0
       for (i in 1:n) {
       for (j in 1:n) {
       tmp <- tmp + abs(x1[i]-x1[j])
       }
       }
      answer = tmp/(n*n)
       return(answer)
      }
      # List of manipulating parameters of experiments
      #L : number of round
      #V : Visible or not
      #A : Income of a rich-group subject
      #B : Income of a poor-group subject
      #R : Probability to be assigned to a rich group
      #I : Number of the same-parameter trial
      #Example
      L = 10
      V = 0
      A = 700
      B = 300
      R = 0.5
      I = 0
      # List of fixed parameters of experiments (assumptions)
      #Rewiring rate = 0.3
      #GINI coefficient (can be known by A or B)
      GINI = 0*as.numeric(A==500) + 0.2*as.numeric(A %in% c(700,850)) + 0.4*as.numeric(A ==1150)
      #Collecting data frame (final output data frame)
      result = data.frame(round=0:L,n_par=NA,n_A=NA,avg_coop=NA,avg_degree=NA,avg_wealth=NA,gini=NA,gmd=NA,avg_coop_A=NA,avg_degree_A=NA,avg_wealth_A=NA,gini_A=NA,gmd_A=NA,avg_coop_B=NA,avg_degree_B=NA,avg_wealth_B=NA,gini_B=NA,gmd_B=NA,isolation=NA,percentIsolation=NA,meanConversionToD=NA,meanConversionToC=NA,degreeLost=NA,degreeLostC=NA,degreeLostD=NA)
      #_A is for a richer group and _B is for a poorer group
      
      
      #####################################################
      # Section 1.5: Practice rounds 1 to 2, to determine C/D in round 1
      
      N = 17 # median of the number of participants over rounds.
      node_rp0 = data.frame(ego_id=1:N, round=0)
      node_import = node_rp0
      
      for (k in 1:2){
        node_rX = node_import #Importing data
        node_rX$round = node_rX$round + 1
        node_rX[is.na(node_rX$prev_degree)==1,"prev_degree"] = 0
        node_rX[is.na(node_rX$prev_local_rate_coop)==1,"prev_local_rate_coop"] = 0
        #Only this calculation needs to change from Round 1
        if (k==1) {
          node_rX$prob_coop = inv.logit(1.099471) 
        } else {
          node_rX$prob_coop = inv.logit((-0.02339288) + (1.46068980)*as.numeric(node_rX$prev_coop==1))
        } 
        
        node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
        
        node_rX$prev_coop = node_rX$coop
          
        assign(paste("coop_rp",k, sep=""),node_rX$coop)
        
        #For the loop
        node_import = node_rX
      }
      
      #cooperation rate in the practice rounds
      coop_rp = apply(cbind(coop_rp1,coop_rp2),1,mean)

      #####################################################
      # Section 2: Round 0 (Agents and environments)
      #Node data generation
      N = 17 # median of the number of participants over rounds.
      node_r0 = data.frame(ego_id=1:N, round=0)
      node_r0$group = sample(c("rich","poor"),N,replace=TRUE,prob=c(R,1-R)) #R is defined as the probability to be assigned to the rich group
      node_r0$initial_wealth = ifelse(node_r0$group=="rich",A,B)
      #Link data generation
      ego_list = NULL
      for (i in 1:N) { ego_list = c(ego_list,rep(i,N)) }
      link_r0 = data.frame(ego_id=ego_list,alt_id=rep(1:N,N))
      link_r0 = link_r0[(link_r0$ego_id < link_r0$alt_id),] #The link was bidirectional, and thus the half and self are omitted.
      link_r0$connected = sample(0:1,dim(link_r0)[1],replace=TRUE,prob=c(0.7,0.3)) #Initial rewiring rate is fixed, 0.3
      link_r0c_ego = link_r0[link_r0$connected==1,]
      link_r0c_alt = link_r0[link_r0$connected==1,]
      colnames(link_r0c_alt) = c("alt_id","ego_id","connected")
      link_r0c = rbind(link_r0c_ego,link_r0c_alt) #this is bidirectional (double counted) for connected ties.
      link_r0c = link_r0c[order(link_r0c$ego_id),]
      link_r0c$alternumber = NA #putting the number for each alter in the same ego
      link_r0c[1,]$alternumber = 1
      for (i in 1:(dim(link_r0c)[1]-1))
        {if (link_r0c[i,]$ego_id == link_r0c[i+1,]$ego_id)
          {link_r0c[i+1,]$alternumber = link_r0c[i,]$alternumber + 1}
        else
          {link_r0c[i+1,]$alternumber = 1}
        #print(i)
        }
      link_r0c2 = reshape(link_r0c, direction = "wide", idvar=c("ego_id","connected"), timevar="alternumber")
      link_r0c2$initial_degree = apply(link_r0c2[,colnames(link_r0c2)[substr(colnames(link_r0c2),1,6) == "alt_id"]],1,function(x){length(na.omit(x))}) #Degree of each ego
      link_r0c2[is.na(link_r0c2$initial_degree)==1,"initial_degree"] = 0
      #Reflect the degree and initial local gini coefficient into the node data
      node_r0 = merge(x=node_r0,y=link_r0c2,all.x=TRUE,all.y=FALSE,by="ego_id")
      node_r0$initial_avg_env_wealth = NA
      node_r0$initial_local_gini = NA #local gini coefficient of the ego and connecting alters
      node_r0$initial_rel_rank = NA #local rank of ego among the ego and connecting alters (divided by the number of the go and connecting alters)
      for (i in 1:(dim(node_r0)[1])){
        node_r0[i,]$initial_avg_env_wealth = mean(na.omit(node_r0[node_r0$ego_id %in%
        node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6) %in% c("ego_id","alt_id")]],"initial_wealth"]))
        node_r0[i,]$initial_local_gini = gini(na.omit(node_r0[node_r0$ego_id %in% node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6)
        %in% c("ego_id","alt_id")]],"initial_wealth"]))
        node_r0[i,]$initial_rel_rank = rank1(na.omit(node_r0[node_r0$ego_id %in% node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6)
        %in% c("ego_id","alt_id")]],"initial_wealth"]))/length(na.omit(node_r0[node_r0$ego_id %in%
        node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6) %in% c("ego_id","alt_id")]],"initial_wealth"]))
        }
      #Finalization of round 0 and Visualization
      #plot(graph.data.frame(link_r0[link_r0$connected==1,],directed=F)) #plot.igraph
      node_r0$everIsolated = 0
      node_r0$maxDegreeLost = NA
      result[result$round==0,2:25] = c(length(node_r0$ego_id),length(node_r0[node_r0$group=="rich",]$ego_id),NA,mean(node_r0$initial_degree),mean(node_r0$initial_wealth),gini(node_r0$initial_wealth),gmd(node_r0$initial_wealth),NA,mean(node_r0[node_r0$group=="rich",]$initial_degree),mean(node_r0[node_r0$group=="rich",]$initial_wealth),gini(node_r0[node_r0$group=="rich",]$initial_wealth),gmd(node_r0[node_r0$group=="rich",]$initial_wealth),NA,mean(node_r0[node_r0$group=="poor",]$initial_degree),mean(node_r0[node_r0$group=="poor",]$initial_wealth),gini(node_r0[node_r0$group=="poor",]$initial_wealth),gmd(node_r0[node_r0$group=="poor",]$initial_wealth),
                                       as.numeric(ifelse(is.na(table(node_r0$initial_degree<=isolationDegree)["TRUE"]),0,1)),
                                       as.numeric(sum(node_r0$everIsolated)/length(node_r0$ego_id)),
                                       NA,
                                       NA,
                                       NA,NA,NA
                                       )
      
      #For the loop at the next round (for round 1, the initial one is the same as the previous [1 prior] one)
      node_import = node_r0
      node_import$initial_coop = NA
      node_import$prev_coop = NA
      node_import$prev_wealth = node_import$initial_wealth
      node_import$prev_degree = node_import$initial_degree
      node_import$prev_avg_env_wealth = node_import$initial_avg_env_wealth
      node_import$prev_local_gini = node_import$initial_local_gini
      node_import$prev_rel_rank = node_import$initial_rel_rank
      node_import$prev_local_rate_coop = NA
      link_import = link_r0
      
      
      #####################################################
      # Section 3: Rounds 1 to 10 or more (behaviors in simulation: the equation of cooperation is different at round 1 because of no history)
      #3-1: Cooperation phase
      for (k in 1:L)
      {
        node_rX = node_import #Importing data
        node_rX$round = node_rX$round + 1
        node_rX[is.na(node_rX$prev_degree)==1,"prev_degree"] = 0
        node_rX[is.na(node_rX$prev_local_rate_coop)==1,"prev_local_rate_coop"] = 0
        #Only this calculation needs to change from Round 1
        if (k==1) {
          node_rX$prob_coop = as.numeric(V==0)*inv.logit((-1.816665) + (2.086067)*coop_rp1 + (1.800153)*coop_rp2) + as.numeric(V==1)*inv.logit((-2.031577) + (2.427157)*coop_rp1 + (1.684193)*coop_rp2 + (-1.528851)*GINI)
          } else {
          node_rX$prob_coop = as.numeric(V==0 & node_rX$prev_coop==0)*inv.logit(-1.039916) + as.numeric(V==0 & node_rX$prev_coop==1)*inv.logit(2.062023) + as.numeric(V==1 & node_rX$prev_coop==0)*inv.logit((-0.2574838)*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (-1.214198)*GINI + (2.508148)*GINI*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (-0.9749075)) + as.numeric(V==1 & node_rX$prev_coop==1)*inv.logit((- 0.6197254)*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (-0.7480261)*GINI + (1.169674)*GINI*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (1.356784))
          } 
        #####manipulate cooperation rate ar round 1 depending on the degree! (keep the total coopertion rate at round 1 constant)
        #####make it so that the nodes with the top fractionCoop*100 percentile of degrees will 100% be a cooperator, but the average percentage of being a cooperator will be the same
        if(k==1){
          prob_coop_df = NULL
          nodesCoop = NULL
          nodesCoop = node_rX$prev_degree>quantile(node_rX$prev_degree,fractionCoop) #assign high-degree nodes to defectors
          prob_coop_df = 
            data.frame(
              prob_coop = node_rX$prob_coop[order(coop_rp)],
              node_number = c(which(nodesCoop),which(!nodesCoop))
            )
          node_rX$prob_coop = prob_coop_df[order(prob_coop_df$node_number),]$prob_coop
          node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
        } else {
          node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
        }
        if (k==1) {
          node_rX$initial_coop = node_rX$coop
          } else {
          node_rX$initial_coop = node_rX$initial_coop
          }
        node_rX$cost = (-50)*node_rX$coop*node_rX$prev_degree
        node_rX$n_coop_received = NA
        for (i in 1:(dim(node_rX)[1]))
          {
          node_rX[i,]$n_coop_received = sum(node_rX[node_rX$ego_id %in% node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) ==
          "alt_id"]],"coop"])
          }
        node_rX$benefit = 100*node_rX$n_coop_received
        node_rX$payoff = node_rX$cost + node_rX$benefit
        node_rX$wealth = node_rX$prev_wealth + node_rX$payoff
        node_rX$rel_rank = NA
        node_rX$local_rate_coop = NA
        for (i in 1:dim(node_rX)[1])
          {
          node_rX[i,]$rel_rank = rank1(na.omit(node_rX[node_rX$ego_id %in% node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) %in%
          c("ego_id","alt_id")]],"wealth"]))/length(na.omit(node_rX[node_rX$ego_id %in%
          node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
          node_rX[i,]$local_rate_coop = mean(na.omit(node_rX[node_rX$ego_id %in% node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) %in%
          c("ego_id","alt_id")]],"coop"]))
          }
        node_rX$growth = as.numeric((node_rX$wealth/node_rX$prev_wealth) > 1)
        node_rX = node_rX[,c("ego_id","round","group","prev_degree","initial_wealth","initial_local_gini","initial_coop","coop","wealth","rel_rank","local_rate_coop","growth","everIsolated","maxDegreeLost")] #Pruning the previous-round data (degree is not updating yet)
        
        #3-2: Rewiring phase
        # 30% of ties (unidirectional) are being rewired
        link_rX_1 = link_import #Importing data (bidirectioanl ego-alter [ego_id < alter_id])
        colnames(link_rX_1) = c("ego_id","alt_id","prev_connected")
        link_rX_1$challenge = sample(0:1,dim(link_rX_1)[1],replace=TRUE,prob=c(0.7,0.3)) # The bidirectional ties being rewired are selected (rewiring rate = 0.3).
        ego_node_data =
        node_rX[,c("ego_id","wealth","coop","prev_degree","initial_wealth","initial_local_gini","initial_coop","rel_rank","local_rate_coop","growth")]
        colnames(ego_node_data) =
        c("ego_id","ego_wealth","ego_coop","ego_prev_degree","ego_initial_wealth","ego_initial_local_gini","ego_initial_coop","ego_rel_rank","ego_local_rate_coop","ego_growth")
        alt_node_data =
        node_rX[,c("ego_id","wealth","coop","prev_degree","initial_wealth","initial_local_gini","initial_coop","rel_rank","local_rate_coop","growth")]
        colnames(alt_node_data) =
        c("alt_id","alt_wealth","alt_coop","alt_prev_degree","alt_initial_wealth","alt_initial_local_gini","alt_initial_coop","alt_rel_rank","alt_local_rate_coop","alt_growth")
        link_rX_2 = merge(x=link_rX_1,y=ego_node_data,all.x=TRUE,all.y=FALSE,by="ego_id")
        link_rX_3 = merge(x=link_rX_2,y=alt_node_data,all.x=TRUE,all.y=FALSE,by="alt_id")
        link_rX_3$choice = sample(c("ego","alt"),dim(link_rX_3)[1],replace=TRUE,prob=c(0.5,0.5)) #decision maker for breaking a link, which is a unilateral decision
        #ego_prob: probability of choosing to connect when challenged (asked)
        link_rX_3$ego_prob = inv.logit((0.5134401)*link_rX_3$prev_connected + (-0.852406)*link_rX_3$ego_coop + (2.96549)*link_rX_3$alt_coop + (-0.1808545))
        link_rX_3$alt_prob = inv.logit((0.5134401)*link_rX_3$prev_connected + (-0.852406)*link_rX_3$alt_coop + (2.96549)*link_rX_3$ego_coop + (-0.1808545))
        link_rX_3$prob_connect = ifelse(link_rX_3$prev_connected == 1, ifelse(link_rX_3$choice == "ego", link_rX_3$ego_prob,
        link_rX_3$alt_prob), link_rX_3$ego_prob*link_rX_3$alt_prob)
        link_rX_3$connect_update = apply(data.frame(link_rX_3$prob_connect),1, function(x) {sample(1:0,1,prob=c(x,(1-x)))})
        link_rX_3$connected = ifelse(link_rX_3$challenge==0,link_rX_3$prev_connected,link_rX_3$connect_update)
        link_rX = link_rX_3[,c("ego_id","alt_id","connected")] #pruning and data is updated
        #Reflect the degree and local gini coefficient into the node data
        link_rXc_ego = link_rX[link_rX$connected==1,]
        link_rXc_alt = link_rX[link_rX$connected==1,]
        colnames(link_rXc_alt) = c("alt_id","ego_id","connected")
        link_rXc = rbind(link_rXc_ego,link_rXc_alt)
        link_rXc = link_rXc[order(link_rXc$ego_id),]
        link_rXc$alternumber = NA
        link_rXc[1,]$alternumber = 1
        for (i in 1:(dim(link_rXc)[1]-1))
          {
            if (link_rXc[i,]$ego_id == link_rXc[i+1,]$ego_id)
            {
            link_rXc[i+1,]$alternumber = link_rXc[i,]$alternumber + 1
            }
            else
            {
            link_rXc[i+1,]$alternumber = 1
            }
            #print(i)
          }
        link_rXc2 = reshape(link_rXc, direction = "wide", idvar=c("ego_id","connected"), timevar="alternumber")
        link_rXc2$degree = apply(link_rXc2[,colnames(link_rXc2)[substr(colnames(link_rXc2),1,3) == "alt"]],1,function(x) {length(na.omit(x))})
        node_rX_final = merge(x=node_rX[,c("ego_id","round","group","initial_wealth","initial_local_gini","initial_coop","coop","wealth","growth","everIsolated","maxDegreeLost")],y=link_rXc2,all.x=TRUE,all.y=FALSE,by="ego_id")
        node_rX_final[is.na(node_rX_final$degree)==1,"degree"] = 0
        node_rX_final$avg_env_wealth = NA
        node_rX_final$local_gini = NA #needs to be updated because the social network changes at the rewiring phase
        node_rX_final$local_rate_coop = NA
        node_rX_final$rel_rank = NA
        for (i in 1:dim(node_rX_final)[1])
          {
          node_rX_final[i,]$avg_env_wealth = mean(na.omit(node_rX_final[node_rX_final$ego_id %in%
          node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
          node_rX_final[i,]$local_gini = gini(na.omit(node_rX_final[node_rX_final$ego_id %in%
          node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
          node_rX_final[i,]$local_rate_coop = mean(na.omit(node_rX_final[node_rX_final$ego_id %in%
          node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"coop"]))
          node_rX_final[i,]$rel_rank = rank1(na.omit(node_rX_final[node_rX_final$ego_id %in%
          node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in%
          c("ego_id","alt_id")]],"wealth"]))/length(na.omit(node_rX_final[node_rX_final$ego_id %in%
          node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
          node_rX_final[i,]$everIsolated = ifelse(node_rX_final[i,]$everIsolated==1,1,ifelse(node_rX_final[i,]$degree<=isolationDegree,1,0))
          node_rX_final[i,]$maxDegreeLost = pmax(node_r0[i,]$initial_degree - node_rX_final[i,]$degree, node_rX_final[i,]$maxDegreeLost, na.rm=TRUE)
        }
        
        
        #Finalization of round X and Visualization
        #plot(graph.data.frame(link_rX[link_rX$connected==1,],directed=F)) #plot.igraph
        result[result$round==k,2:25] =
        c(length(node_rX_final$ego_id),length(node_rX_final[node_rX_final$group=="rich",]$ego_id),mean(node_rX_final$coop),mean(node_rX_final$degree),mean(node_rX_final$wealth),gini(node_rX_final$wealth),gmd(node_rX_final$wealth),mean(node_rX_final[node_rX_final$group=="rich",]$coop),mean(node_rX_final[node_rX_final$group=="rich",]$degree),mean(node_rX_final[node_rX_final$group=="rich",]$wealth),gini(node_rX_final[node_rX_final$group=="rich",]$wealth),gmd(node_rX_final[node_rX_final$group=="rich",]$wealth),mean(node_rX_final[node_rX_final$group=="poor",]$coop),mean(node_rX_final[node_rX_final$group=="poor",]$degree),mean(node_rX_final[node_rX_final$group=="poor",]$wealth),gini(node_rX_final[node_rX_final$group=="poor",]$wealth),gmd(node_rX_final[node_rX_final$group=="poor",]$wealth),
                                       as.numeric(ifelse(is.na(table(node_rX_final$degree<=isolationDegree)["TRUE"]),0,1)),
                                       as.numeric(sum(node_rX_final$everIsolated)/length(node_rX_final$ego_id)),
          prop.table(table(node_rX_final[node_rX_final$initial_coop==1]$coop))["0"],
          prop.table(table(node_rX_final[node_rX_final$initial_coop==0]$coop))["1"],
          suppressWarnings({mean(node_rX_final$maxDegreeLost,na.rm=TRUE)}),
          suppressWarnings({mean(node_rX_final[node_rX_final$initial_coop==1]$maxDegreeLost,na.rm=TRUE)}),
          suppressWarnings({mean(node_rX_final[node_rX_final$initial_coop==0]$maxDegreeLost,na.rm=TRUE)})
          )
        
        #For the loop
        node_import = node_rX_final
        colnames(node_import)[colnames(node_import) %in%
        c("coop","wealth","growth","degree","avg_env_wealth","local_gini","local_rate_coop","rel_rank")] =
        c("prev_coop","prev_wealth","prev_growth","prev_degree","prev_avg_env_wealth","prev_local_gini","prev_local_rate_coop","prev_rel_rank")
        link_import = link_rX
        #print(paste0("Round ",k," is done."))
      }
      
      coopFrac[m] = fractionCoop
      avgCoop[m] = result[result$round==1,]$avg_coop
      avgCoopFinal[m] = result[result$round==10,]$avg_coop
      percentIsolation[m] = max(result[result$round>=1,]$percentIsolation)
      isolation[m] = max(result[result$round>=1,]$isolation)
      percentIsolatedD[m] = prop.table(table(node_rX_final[node_rX_final$everIsolated==1,]$initial_coop))["0"]
      
      
      link_rX_final = data.table::melt(setDT(node_rX_final),
                               measure = patterns('alt_id'),
                               variable.name = 'linkNumber', 
                               value.name = c('alt_id'))
      link_rX_final = data.frame(link_rX_final)[c("ego_id","alt_id")]
      link_rX_final = link_rX_final[complete.cases(link_rX_final),]
      link_rX_final = data.frame(t(unique(apply(link_rX_final, 1, function(x) sort(x))))) %>% distinct(X1, X2)
      node_g_final = data.frame(node_rX_final)[c("ego_id","initial_coop","coop")]
      node_g_final$initial_coop = factor(node_g_final$initial_coop)
      g_rX_final = graph_from_data_frame(link_rX_final, directed = FALSE, vertices=node_g_final)
      g_r0 = graph_from_data_frame(link_r0[link_r0$connected==1,][1:2], directed = FALSE, vertices=node_r0)
      nCommunities[m] = max(membership(cluster_louvain(g_rX_final)),na.rm=TRUE)
      communitySize[m] = mean(table(membership(cluster_louvain(g_rX_final))),na.rm=TRUE)
      assortativityInitial[m] = assortativity(g_r0, V(g_rX_final)$initial_coop == 1)
      assortativityFinal[m] = assortativity(g_rX_final, V(g_rX_final)$initial_coop == 1)
      conversionRate[m] = prop.table(table(V(g_rX_final)$coop == V(g_rX_final)$initial_coop))["FALSE"]
      conversionToD[m] = prop.table(table(V(g_rX_final)$coop[V(g_rX_final)$initial_coop==1]))["0"]
      conversionToC[m] = prop.table(table(V(g_rX_final)$coop[V(g_rX_final)$initial_coop==0]))["1"]
      degreeC[m] = mean(igraph::degree(g_rX_final)[V(g_rX_final)$coop==1],na.rm=TRUE)
      degreeD[m] = mean(igraph::degree(g_rX_final)[V(g_rX_final)$coop==0],na.rm=TRUE)
      meanConversionToD[m] = mean(result[result$round>=2,]$meanConversionToD, na.rm=TRUE)
      meanConversionToC[m] = mean(result[result$round>=2,]$meanConversionToC, na.rm=TRUE)
      degreeLost[m] = result[result$round==10,]$degreeLost
      degreeLostC[m] = result[result$round==10,]$degreeLostC
      degreeLostD[m] = result[result$round==10,]$degreeLostD
  
    }
    
df.netIntLowDegree = rbind(df.netIntLowDegree,
                  data.frame(
                    coopFrac = coopFrac,
                    avgCoop = avgCoop,
                    avgCoopFinal = avgCoopFinal,
                    percentIsolation = percentIsolation,
                    isolation = isolation,
                    percentIsolatedD = percentIsolatedD,
                    nCommunities = nCommunities,
                    communitySize = communitySize,
                    assortativityInitial = assortativityInitial,
                    assortativityFinal = assortativityFinal,
                    conversionRate = conversionRate,
                    conversionToD = conversionToD, 
                    conversionToC = conversionToC, 
                    degreeC = degreeC, 
                    degreeD = degreeD,
                    meanConversionToD = meanConversionToD, 
                    meanConversionToC = meanConversionToC,
                    degreeLost = degreeLost,
                    degreeLostC = degreeLostC,
                    degreeLostD = degreeLostD
                    ))
plot(g_r0,vertex.color=V(g_rX_final)$initial_coop,vertex.label=ifelse(is.na(V(g_rX_final)$initial_coop),"NA",ifelse(V(g_rX_final)$initial_coop==1,"C","D")),main=paste("fracCoop=",frac,", round 0",sep=""))
plot(g_rX_final,vertex.color=V(g_rX_final)$initial_coop,vertex.label=ifelse(is.na(V(g_rX_final)$initial_coop),"NA",ifelse(V(g_rX_final)$initial_coop==1,"C","D")),main=paste("fracCoop=",frac,", final round",sep=""))
}

sum.netIntLowDegree <- data.frame(
  df.netIntLowDegree %>% 
    group_by(coopFrac) %>%
      summarise(
        mean.isolation = mean(isolation),
        ci.isolation   = 1.96 * sd(isolation)/sqrt(n()),
        mean.percentIsolation = mean(percentIsolation),
        ci.percentIsolation   = 1.96 * sd(percentIsolation)/sqrt(n()),
        mean.percentIsolatedD = mean(percentIsolatedD,na.rm=TRUE),
        ci.percentIsolatedD   = 1.96 * sd(percentIsolatedD,na.rm=TRUE)/sqrt(sum(isolation)),
        mean.avgCoop = mean(avgCoop,na.rm=TRUE),
        ci.avgCoop   = 1.96 * sd(avgCoop,na.rm=TRUE)/sqrt(n()),
        mean.avgCoopFinal = mean(avgCoopFinal,na.rm=TRUE),
        ci.avgCoopFinal   = 1.96 * sd(avgCoopFinal,na.rm=TRUE)/sqrt(n()),
        mean.nCommunities = mean(nCommunities,na.rm=TRUE),
        ci.nCommunities   = 1.96 * sd(nCommunities,na.rm=TRUE)/sqrt(n()),
        mean.communitySize = mean(communitySize,na.rm=TRUE),
        ci.communitySize   = 1.96 * sd(communitySize,na.rm=TRUE)/sqrt(n()),
        mean.assortativityInitial = mean(assortativityInitial,na.rm=TRUE),
        ci.assortativityInitial   = 1.96 * sd(assortativityInitial,na.rm=TRUE)/sqrt(n()),
        mean.assortativityFinal = mean(assortativityFinal,na.rm=TRUE),
        ci.assortativityFinal   = 1.96 * sd(assortativityFinal,na.rm=TRUE)/sqrt(n()),
        mean.conversionRate = mean(conversionRate,na.rm=TRUE),
        ci.conversionRate   = 1.96 * sd(conversionRate,na.rm=TRUE)/sqrt(n()),
        mean.conversionToD = mean(conversionToD,na.rm=TRUE),
        ci.conversionToD   = 1.96 * sd(conversionToD,na.rm=TRUE)/sqrt(n()),
        mean.conversionToC = mean(conversionToC,na.rm=TRUE),
        ci.conversionToC   = 1.96 * sd(conversionToC,na.rm=TRUE)/sqrt(n()),
        mean.degreeC = mean(degreeC,na.rm=TRUE),
        ci.degreeC   = 1.96 * sd(degreeC,na.rm=TRUE)/sqrt(n()),
        mean.degreeD = mean(degreeD,na.rm=TRUE),
        ci.degreeD   = 1.96 * sd(degreeD,na.rm=TRUE)/sqrt(n()),
        mean.meanConversionToD = mean(meanConversionToD,na.rm=TRUE),
        ci.meanConversionToD   = 1.96 * sd(meanConversionToD,na.rm=TRUE)/sqrt(n()),
        mean.meanConversionToC = mean(meanConversionToC,na.rm=TRUE),
        ci.meanConversionToC   = 1.96 * sd(meanConversionToC,na.rm=TRUE)/sqrt(n()),
        mean.degreeLost = mean(degreeLost,na.rm=TRUE),
        ci.degreeLost   = 1.96 * sd(degreeLost,na.rm=TRUE)/sqrt(n()),
        mean.degreeLostC = mean(degreeLostC,na.rm=TRUE),
        ci.degreeLostC   = 1.96 * sd(degreeLostC,na.rm=TRUE)/sqrt(n()),
        mean.degreeLostD = mean(degreeLostD,na.rm=TRUE),
        ci.degreeLostD   = 1.96 * sd(degreeLostD,na.rm=TRUE)/sqrt(n())
        )
  )

kable(sum.netIntLowDegree[c(1:9)]) %>% kableExtra::kable_styling(font_size = 10)
coopFrac mean.isolation ci.isolation mean.percentIsolation ci.percentIsolation mean.percentIsolatedD ci.percentIsolatedD mean.avgCoop ci.avgCoop
0.0 0.534 0.0437693 0.0483529 0.0051050 0.8820000 0.0253886 0.7020000 0.0095360
0.1 0.492 0.0438652 0.0428235 0.0048472 0.8513131 0.0281204 0.6967059 0.0094264
0.2 0.478 0.0438283 0.0428235 0.0048362 0.8408602 0.0299159 0.6928235 0.0090414
0.3 0.472 0.0438020 0.0404706 0.0045192 0.8464612 0.0296199 0.6962353 0.0101417
0.4 0.450 0.0436509 0.0374118 0.0043780 0.8695238 0.0296215 0.7015294 0.0098748
0.5 0.508 0.0438652 0.0421176 0.0045094 0.8714286 0.0282612 0.6871765 0.0104468
0.6 0.428 0.0434136 0.0355294 0.0043280 0.8897619 0.0273302 0.6983529 0.0091975
0.7 0.488 0.0438582 0.0414118 0.0046634 0.8734807 0.0274426 0.7003529 0.0099026
0.8 0.502 0.0438705 0.0434118 0.0047567 0.8842491 0.0257182 0.6984706 0.0093162
0.9 0.522 0.0438283 0.0470588 0.0050359 0.8815104 0.0252434 0.6957647 0.0102063
1.0 0.572 0.0434136 0.0515294 0.0051526 0.8949430 0.0233945 0.6980000 0.0091050
kable(sum.netIntLowDegree[c(1,10:17)]) %>% kableExtra::kable_styling(font_size = 10) 
coopFrac mean.avgCoopFinal ci.avgCoopFinal mean.nCommunities ci.nCommunities mean.communitySize ci.communitySize mean.assortativityInitial ci.assortativityInitial
0.0 0.7064706 0.0097080 2.680 0.0455581 6.610167 0.1221974 -0.0680666 0.0106503
0.1 0.7089412 0.0097891 2.658 0.0458475 6.669667 0.1234736 -0.0762168 0.0115542
0.2 0.6922353 0.0099843 2.670 0.0434388 6.618667 0.1197691 -0.0763244 0.0113223
0.3 0.7015294 0.0093767 2.688 0.0456473 6.590333 0.1219728 -0.0782161 0.0113961
0.4 0.7024706 0.0091845 2.734 0.0453586 6.471333 0.1193141 -0.0775183 0.0107402
0.5 0.7011765 0.0096691 2.676 0.0473337 6.635667 0.1248058 -0.0892189 0.0110488
0.6 0.6935294 0.0100954 2.684 0.0461077 6.604500 0.1227847 -0.0816845 0.0105861
0.7 0.6969412 0.0095628 2.706 0.0457238 6.545000 0.1212553 -0.0829724 0.0108386
0.8 0.7037647 0.0098993 2.692 0.0451794 6.576167 0.1211429 -0.0841300 0.0106377
0.9 0.6948235 0.0100090 2.680 0.0472177 6.624333 0.1244885 -0.0756449 0.0104771
1.0 0.6929412 0.0094264 2.698 0.0442896 6.553500 0.1196212 -0.0637183 0.0108137
kable(sum.netIntLowDegree[c(1,18:25)]) %>% kableExtra::kable_styling(font_size = 10) 
coopFrac mean.assortativityFinal ci.assortativityFinal mean.conversionRate ci.conversionRate mean.conversionToD ci.conversionToD mean.conversionToC ci.conversionToC
0.0 -0.0601173 0.0050759 0.4098824 0.0108054 0.2969414 0.0113675 0.7044995 0.0179618
0.1 -0.0597906 0.0054437 0.4115294 0.0103432 0.2936636 0.0110759 0.7144386 0.0176359
0.2 -0.0503202 0.0053503 0.4217647 0.0104318 0.3096367 0.0115371 0.6997502 0.0180863
0.3 -0.0600859 0.0056878 0.4088235 0.0106621 0.2955999 0.0110410 0.6911301 0.0180246
0.4 -0.0592825 0.0056122 0.4061176 0.0098129 0.2918831 0.0104438 0.6938355 0.0176233
0.5 -0.0586738 0.0051416 0.4130588 0.0101265 0.2958117 0.0109472 0.6939716 0.0181083
0.6 -0.0628713 0.0053007 0.4165882 0.0103731 0.3094895 0.0116267 0.6964534 0.0173008
0.7 -0.0542421 0.0050247 0.4144706 0.0108065 0.3025699 0.0115112 0.6956598 0.0186308
0.8 -0.0624092 0.0050828 0.4109412 0.0106750 0.2988387 0.0112641 0.7089753 0.0179251
0.9 -0.0613772 0.0050869 0.4202353 0.0105412 0.3083202 0.0115890 0.7005078 0.0183845
1.0 -0.0544468 0.0051438 0.4234118 0.0101859 0.3146695 0.0111761 0.7011403 0.0170390
kable(sum.netIntLowDegree[c(1,26:33)]) %>% kableExtra::kable_styling(font_size = 10) 
coopFrac mean.degreeC ci.degreeC mean.degreeD ci.degreeD mean.meanConversionToD ci.meanConversionToD mean.meanConversionToC ci.meanConversionToC
0.0 11.15293 0.0842314 8.798630 0.1220733 0.2882813 0.0062858 0.6894838 0.0055363
0.1 11.13611 0.0859909 8.745455 0.1199678 0.2896810 0.0060989 0.6945557 0.0059284
0.2 11.02332 0.0836469 8.710410 0.1148853 0.3052366 0.0060515 0.6836132 0.0059800
0.3 11.10895 0.0837283 8.673293 0.1125271 0.2983350 0.0059521 0.6904681 0.0055438
0.4 11.07988 0.0861083 8.779520 0.1154978 0.3072522 0.0055964 0.6924572 0.0056737
0.5 11.10778 0.0810545 8.700841 0.1170698 0.2987529 0.0054215 0.6886403 0.0063998
0.6 11.14050 0.0796611 8.730448 0.1173215 0.2965398 0.0056675 0.6892624 0.0062200
0.7 11.12280 0.0809654 8.715066 0.1196057 0.2944513 0.0063634 0.6891181 0.0054629
0.8 11.15737 0.0803654 8.838578 0.1179162 0.2977624 0.0060522 0.7034314 0.0053941
0.9 11.12858 0.0816694 8.682216 0.1191260 0.2975375 0.0059962 0.6861637 0.0061962
1.0 11.05628 0.0882381 8.631702 0.1132754 0.3001706 0.0060890 0.6768988 0.0060648
kable(sum.netIntLowDegree[c(1,34:ncol(sum.netIntLowDegree))]) %>% kableExtra::kable_styling(font_size = 10) 
coopFrac mean.degreeLost ci.degreeLost mean.degreeLostC ci.degreeLostC mean.degreeLostD ci.degreeLostD
0.0 -0.8638270 0.0557797 -0.8870969 0.0550647 -0.6963597 0.0590952
0.1 -0.8419201 0.0550951 -0.8867780 0.0559373 -0.5954927 0.0446931
0.2 -0.8298132 0.0537815 -0.8793214 0.0516904 -0.6640686 0.0581742
0.3 -0.8454632 0.0541031 -0.8672910 0.0516400 -0.7748006 0.0612344
0.4 -0.8494010 0.0568431 -0.8863144 0.0566937 -0.7229807 0.0561996
0.5 -0.8355755 0.0537886 -0.9054340 0.0541975 -0.6260000 0.0482681
0.6 -0.8266025 0.0511002 -0.8650398 0.0523143 -0.7075096 0.0457852
0.7 -0.8841471 0.0554158 -0.9138668 0.0549651 -0.7590763 0.0562492
0.8 -0.8793647 0.0546997 -0.8738623 0.0554822 -0.9019358 0.0515906
0.9 -0.8614485 0.0531698 -0.9018003 0.0529824 -0.7059180 0.0519192
1.0 -0.8926676 0.0535594 -0.9202454 0.0530454 -0.7260356 0.0547031
compare_means(percentIsolation ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.              group1 group2        p p.adj p.format p.signif method  
##    <chr>            <chr>  <chr>     <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 percentIsolation 0      0.1    0.106    1     0.10618  ns       Wilcoxon
##  2 percentIsolation 0      0.2    0.0956   1     0.09562  ns       Wilcoxon
##  3 percentIsolation 0      0.3    0.0365   1     0.03655  *        Wilcoxon
##  4 percentIsolation 0      0.4    0.00250  0.12  0.00250  **       Wilcoxon
##  5 percentIsolation 0      0.5    0.158    1     0.15813  ns       Wilcoxon
##  6 percentIsolation 0      0.6    0.000234 0.012 0.00023  ***      Wilcoxon
##  7 percentIsolation 0      0.7    0.0617   1     0.06171  ns       Wilcoxon
##  8 percentIsolation 0      0.8    0.198    1     0.19798  ns       Wilcoxon
##  9 percentIsolation 0      0.9    0.714    1     0.71353  ns       Wilcoxon
## 10 percentIsolation 0      1      0.303    1     0.30291  ns       Wilcoxon
## # … with 45 more rows
compare_means(avgCoop ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.     group1 group2      p p.adj p.format p.signif method  
##    <chr>   <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 avgCoop 0      0.1    0.462      1 0.462    ns       Wilcoxon
##  2 avgCoop 0      0.2    0.183      1 0.183    ns       Wilcoxon
##  3 avgCoop 0      0.3    0.487      1 0.487    ns       Wilcoxon
##  4 avgCoop 0      0.4    0.968      1 0.968    ns       Wilcoxon
##  5 avgCoop 0      0.5    0.0869     1 0.087    ns       Wilcoxon
##  6 avgCoop 0      0.6    0.517      1 0.517    ns       Wilcoxon
##  7 avgCoop 0      0.7    0.982      1 0.982    ns       Wilcoxon
##  8 avgCoop 0      0.8    0.665      1 0.665    ns       Wilcoxon
##  9 avgCoop 0      0.9    0.509      1 0.509    ns       Wilcoxon
## 10 avgCoop 0      1      0.459      1 0.459    ns       Wilcoxon
## # … with 45 more rows
compare_means(avgCoopFinal ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.          group1 group2      p p.adj p.format p.signif method  
##    <chr>        <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 avgCoopFinal 0      0.1    0.612      1 0.612    ns       Wilcoxon
##  2 avgCoopFinal 0      0.2    0.0580     1 0.058    ns       Wilcoxon
##  3 avgCoopFinal 0      0.3    0.468      1 0.468    ns       Wilcoxon
##  4 avgCoopFinal 0      0.4    0.553      1 0.553    ns       Wilcoxon
##  5 avgCoopFinal 0      0.5    0.543      1 0.543    ns       Wilcoxon
##  6 avgCoopFinal 0      0.6    0.110      1 0.110    ns       Wilcoxon
##  7 avgCoopFinal 0      0.7    0.160      1 0.160    ns       Wilcoxon
##  8 avgCoopFinal 0      0.8    0.786      1 0.786    ns       Wilcoxon
##  9 avgCoopFinal 0      0.9    0.0880     1 0.088    ns       Wilcoxon
## 10 avgCoopFinal 0      1      0.0330     1 0.033    *        Wilcoxon
## # … with 45 more rows
compare_means(nCommunities ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.          group1 group2     p p.adj p.format p.signif method  
##    <chr>        <chr>  <chr>  <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 nCommunities 0      0.1    0.502     1 0.502    ns       Wilcoxon
##  2 nCommunities 0      0.2    0.864     1 0.864    ns       Wilcoxon
##  3 nCommunities 0      0.3    0.816     1 0.816    ns       Wilcoxon
##  4 nCommunities 0      0.4    0.106     1 0.106    ns       Wilcoxon
##  5 nCommunities 0      0.5    0.819     1 0.819    ns       Wilcoxon
##  6 nCommunities 0      0.6    0.933     1 0.933    ns       Wilcoxon
##  7 nCommunities 0      0.7    0.446     1 0.446    ns       Wilcoxon
##  8 nCommunities 0      0.8    0.703     1 0.703    ns       Wilcoxon
##  9 nCommunities 0      0.9    0.918     1 0.918    ns       Wilcoxon
## 10 nCommunities 0      1      0.538     1 0.538    ns       Wilcoxon
## # … with 45 more rows
compare_means(communitySize ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.           group1 group2     p p.adj p.format p.signif method  
##    <chr>         <chr>  <chr>  <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 communitySize 0      0.1    0.502     1 0.502    ns       Wilcoxon
##  2 communitySize 0      0.2    0.864     1 0.864    ns       Wilcoxon
##  3 communitySize 0      0.3    0.816     1 0.816    ns       Wilcoxon
##  4 communitySize 0      0.4    0.106     1 0.106    ns       Wilcoxon
##  5 communitySize 0      0.5    0.819     1 0.819    ns       Wilcoxon
##  6 communitySize 0      0.6    0.933     1 0.933    ns       Wilcoxon
##  7 communitySize 0      0.7    0.446     1 0.446    ns       Wilcoxon
##  8 communitySize 0      0.8    0.703     1 0.703    ns       Wilcoxon
##  9 communitySize 0      0.9    0.918     1 0.918    ns       Wilcoxon
## 10 communitySize 0      1      0.538     1 0.538    ns       Wilcoxon
## # … with 45 more rows
compare_means(assortativityInitial ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.                  group1 group2      p p.adj p.format p.signif method  
##    <chr>                <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 assortativityInitial 0      0.1    0.401      1 0.4013   ns       Wilcoxon
##  2 assortativityInitial 0      0.2    0.264      1 0.2640   ns       Wilcoxon
##  3 assortativityInitial 0      0.3    0.370      1 0.3702   ns       Wilcoxon
##  4 assortativityInitial 0      0.4    0.393      1 0.3929   ns       Wilcoxon
##  5 assortativityInitial 0      0.5    0.0316     1 0.0316   *        Wilcoxon
##  6 assortativityInitial 0      0.6    0.0913     1 0.0913   ns       Wilcoxon
##  7 assortativityInitial 0      0.7    0.0971     1 0.0971   ns       Wilcoxon
##  8 assortativityInitial 0      0.8    0.103      1 0.1035   ns       Wilcoxon
##  9 assortativityInitial 0      0.9    0.571      1 0.5707   ns       Wilcoxon
## 10 assortativityInitial 0      1      0.298      1 0.2981   ns       Wilcoxon
## # … with 45 more rows
compare_means(assortativityFinal ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.                group1 group2       p p.adj p.format p.signif method  
##    <chr>              <chr>  <chr>    <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 assortativityFinal 0      0.1    0.744    1    0.7441   ns       Wilcoxon
##  2 assortativityFinal 0      0.2    0.00425  0.22 0.0042   **       Wilcoxon
##  3 assortativityFinal 0      0.3    0.887    1    0.8867   ns       Wilcoxon
##  4 assortativityFinal 0      0.4    0.582    1    0.5820   ns       Wilcoxon
##  5 assortativityFinal 0      0.5    0.652    1    0.6523   ns       Wilcoxon
##  6 assortativityFinal 0      0.6    0.399    1    0.3988   ns       Wilcoxon
##  7 assortativityFinal 0      0.7    0.0870   1    0.0870   ns       Wilcoxon
##  8 assortativityFinal 0      0.8    0.702    1    0.7023   ns       Wilcoxon
##  9 assortativityFinal 0      0.9    0.850    1    0.8505   ns       Wilcoxon
## 10 assortativityFinal 0      1      0.0748   1    0.0748   ns       Wilcoxon
## # … with 45 more rows
compare_means(conversionRate ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.            group1 group2     p p.adj p.format p.signif method  
##    <chr>          <chr>  <chr>  <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 conversionRate 0      0.1    0.807     1 0.807    ns       Wilcoxon
##  2 conversionRate 0      0.2    0.143     1 0.143    ns       Wilcoxon
##  3 conversionRate 0      0.3    0.850     1 0.850    ns       Wilcoxon
##  4 conversionRate 0      0.4    0.578     1 0.578    ns       Wilcoxon
##  5 conversionRate 0      0.5    0.929     1 0.929    ns       Wilcoxon
##  6 conversionRate 0      0.6    0.505     1 0.505    ns       Wilcoxon
##  7 conversionRate 0      0.7    0.828     1 0.828    ns       Wilcoxon
##  8 conversionRate 0      0.8    0.951     1 0.951    ns       Wilcoxon
##  9 conversionRate 0      0.9    0.258     1 0.258    ns       Wilcoxon
## 10 conversionRate 0      1      0.155     1 0.155    ns       Wilcoxon
## # … with 45 more rows
compare_means(conversionToD ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.           group1 group2      p p.adj p.format p.signif method  
##    <chr>         <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 conversionToD 0      0.1    0.743      1 0.7426   ns       Wilcoxon
##  2 conversionToD 0      0.2    0.144      1 0.1439   ns       Wilcoxon
##  3 conversionToD 0      0.3    0.828      1 0.8279   ns       Wilcoxon
##  4 conversionToD 0      0.4    0.734      1 0.7344   ns       Wilcoxon
##  5 conversionToD 0      0.5    0.868      1 0.8681   ns       Wilcoxon
##  6 conversionToD 0      0.6    0.197      1 0.1974   ns       Wilcoxon
##  7 conversionToD 0      0.7    0.602      1 0.6017   ns       Wilcoxon
##  8 conversionToD 0      0.8    0.800      1 0.8001   ns       Wilcoxon
##  9 conversionToD 0      0.9    0.218      1 0.2180   ns       Wilcoxon
## 10 conversionToD 0      1      0.0308     1 0.0308   *        Wilcoxon
## # … with 45 more rows
compare_means(conversionToC ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.           group1 group2     p p.adj p.format p.signif method  
##    <chr>         <chr>  <chr>  <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 conversionToC 0      0.1    0.423     1 0.423    ns       Wilcoxon
##  2 conversionToC 0      0.2    0.628     1 0.628    ns       Wilcoxon
##  3 conversionToC 0      0.3    0.301     1 0.301    ns       Wilcoxon
##  4 conversionToC 0      0.4    0.354     1 0.354    ns       Wilcoxon
##  5 conversionToC 0      0.5    0.401     1 0.401    ns       Wilcoxon
##  6 conversionToC 0      0.6    0.589     1 0.589    ns       Wilcoxon
##  7 conversionToC 0      0.7    0.544     1 0.544    ns       Wilcoxon
##  8 conversionToC 0      0.8    0.780     1 0.780    ns       Wilcoxon
##  9 conversionToC 0      0.9    0.749     1 0.749    ns       Wilcoxon
## 10 conversionToC 0      1      0.605     1 0.605    ns       Wilcoxon
## # … with 45 more rows
compare_means(degreeC ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.     group1 group2      p p.adj p.format p.signif method  
##    <chr>   <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 degreeC 0      0.1    0.925      1 0.925    ns       Wilcoxon
##  2 degreeC 0      0.2    0.0868     1 0.087    ns       Wilcoxon
##  3 degreeC 0      0.3    0.634      1 0.634    ns       Wilcoxon
##  4 degreeC 0      0.4    0.521      1 0.521    ns       Wilcoxon
##  5 degreeC 0      0.5    0.612      1 0.612    ns       Wilcoxon
##  6 degreeC 0      0.6    0.792      1 0.792    ns       Wilcoxon
##  7 degreeC 0      0.7    0.794      1 0.794    ns       Wilcoxon
##  8 degreeC 0      0.8    0.769      1 0.769    ns       Wilcoxon
##  9 degreeC 0      0.9    0.897      1 0.897    ns       Wilcoxon
## 10 degreeC 0      1      0.210      1 0.210    ns       Wilcoxon
## # … with 45 more rows
compare_means(degreeD ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.     group1 group2      p p.adj p.format p.signif method  
##    <chr>   <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 degreeD 0      0.1    0.531      1 0.531    ns       Wilcoxon
##  2 degreeD 0      0.2    0.414      1 0.414    ns       Wilcoxon
##  3 degreeD 0      0.3    0.236      1 0.236    ns       Wilcoxon
##  4 degreeD 0      0.4    0.977      1 0.977    ns       Wilcoxon
##  5 degreeD 0      0.5    0.197      1 0.197    ns       Wilcoxon
##  6 degreeD 0      0.6    0.802      1 0.802    ns       Wilcoxon
##  7 degreeD 0      0.7    0.411      1 0.411    ns       Wilcoxon
##  8 degreeD 0      0.8    0.528      1 0.528    ns       Wilcoxon
##  9 degreeD 0      0.9    0.282      1 0.282    ns       Wilcoxon
## 10 degreeD 0      1      0.0726     1 0.073    ns       Wilcoxon
## # … with 45 more rows
compare_means(meanConversionToD ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.               group1 group2        p p.adj p.format p.signif method  
##    <chr>             <chr>  <chr>     <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 meanConversionToD 0      0.1    0.841     1    0.84080  ns       Wilcoxon
##  2 meanConversionToD 0      0.2    0.00281   0.15 0.00281  **       Wilcoxon
##  3 meanConversionToD 0      0.3    0.0625    1    0.06246  ns       Wilcoxon
##  4 meanConversionToD 0      0.4    0.000191  0.01 0.00019  ***      Wilcoxon
##  5 meanConversionToD 0      0.5    0.0543    1    0.05428  ns       Wilcoxon
##  6 meanConversionToD 0      0.6    0.0925    1    0.09246  ns       Wilcoxon
##  7 meanConversionToD 0      0.7    0.223     1    0.22310  ns       Wilcoxon
##  8 meanConversionToD 0      0.8    0.0757    1    0.07567  ns       Wilcoxon
##  9 meanConversionToD 0      0.9    0.0795    1    0.07946  ns       Wilcoxon
## 10 meanConversionToD 0      1      0.0324    1    0.03244  *        Wilcoxon
## # … with 45 more rows
compare_means(meanConversionToC ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.               group1 group2      p p.adj p.format p.signif method  
##    <chr>             <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 meanConversionToC 0      0.1    0.478      1 0.4782   ns       Wilcoxon
##  2 meanConversionToC 0      0.2    0.413      1 0.4126   ns       Wilcoxon
##  3 meanConversionToC 0      0.3    0.888      1 0.8880   ns       Wilcoxon
##  4 meanConversionToC 0      0.4    0.686      1 0.6855   ns       Wilcoxon
##  5 meanConversionToC 0      0.5    0.947      1 0.9471   ns       Wilcoxon
##  6 meanConversionToC 0      0.6    0.985      1 0.9845   ns       Wilcoxon
##  7 meanConversionToC 0      0.7    0.839      1 0.8387   ns       Wilcoxon
##  8 meanConversionToC 0      0.8    0.0641     1 0.0641   ns       Wilcoxon
##  9 meanConversionToC 0      0.9    0.424      1 0.4237   ns       Wilcoxon
## 10 meanConversionToC 0      1      0.122      1 0.1222   ns       Wilcoxon
## # … with 45 more rows
compare_means(degreeLost ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.        group1 group2     p p.adj p.format p.signif method  
##    <chr>      <chr>  <chr>  <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 degreeLost 0      0.1    0.691     1 0.691    ns       Wilcoxon
##  2 degreeLost 0      0.2    0.321     1 0.321    ns       Wilcoxon
##  3 degreeLost 0      0.3    0.744     1 0.744    ns       Wilcoxon
##  4 degreeLost 0      0.4    0.443     1 0.443    ns       Wilcoxon
##  5 degreeLost 0      0.5    0.521     1 0.521    ns       Wilcoxon
##  6 degreeLost 0      0.6    0.202     1 0.202    ns       Wilcoxon
##  7 degreeLost 0      0.7    0.546     1 0.546    ns       Wilcoxon
##  8 degreeLost 0      0.8    0.643     1 0.643    ns       Wilcoxon
##  9 degreeLost 0      0.9    0.977     1 0.977    ns       Wilcoxon
## 10 degreeLost 0      1      0.465     1 0.465    ns       Wilcoxon
## # … with 45 more rows
compare_means(degreeLostC ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.         group1 group2     p p.adj p.format p.signif method  
##    <chr>       <chr>  <chr>  <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 degreeLostC 0      0.1    0.845     1 0.85     ns       Wilcoxon
##  2 degreeLostC 0      0.2    0.752     1 0.75     ns       Wilcoxon
##  3 degreeLostC 0      0.3    0.748     1 0.75     ns       Wilcoxon
##  4 degreeLostC 0      0.4    0.669     1 0.67     ns       Wilcoxon
##  5 degreeLostC 0      0.5    0.546     1 0.55     ns       Wilcoxon
##  6 degreeLostC 0      0.6    0.431     1 0.43     ns       Wilcoxon
##  7 degreeLostC 0      0.7    0.533     1 0.53     ns       Wilcoxon
##  8 degreeLostC 0      0.8    0.817     1 0.82     ns       Wilcoxon
##  9 degreeLostC 0      0.9    0.805     1 0.80     ns       Wilcoxon
## 10 degreeLostC 0      1      0.431     1 0.43     ns       Wilcoxon
## # … with 45 more rows
compare_means(degreeLostD ~ coopFrac, data=df.netIntLowDegree)
## # A tibble: 55 × 8
##    .y.         group1 group2      p p.adj p.format p.signif method  
##    <chr>       <chr>  <chr>   <dbl> <dbl> <chr>    <chr>    <chr>   
##  1 degreeLostD 0      0.1    0.434      1 0.43369  ns       Wilcoxon
##  2 degreeLostD 0      0.2    0.716      1 0.71596  ns       Wilcoxon
##  3 degreeLostD 0      0.3    0.475      1 0.47523  ns       Wilcoxon
##  4 degreeLostD 0      0.4    0.888      1 0.88833  ns       Wilcoxon
##  5 degreeLostD 0      0.5    0.556      1 0.55568  ns       Wilcoxon
##  6 degreeLostD 0      0.6    0.874      1 0.87409  ns       Wilcoxon
##  7 degreeLostD 0      0.7    0.388      1 0.38822  ns       Wilcoxon
##  8 degreeLostD 0      0.8    0.0456     1 0.04565  *        Wilcoxon
##  9 degreeLostD 0      0.9    0.645      1 0.64461  ns       Wilcoxon
## 10 degreeLostD 0      1      0.779      1 0.77881  ns       Wilcoxon
## # … with 45 more rows
g.netIntLowDegree = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="percentIsolation", add = "mean_se") +
  stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.098, method="t.test") +  
  labs(
    title = "Isolation when defectors are assigned to high-degree nodes",
    x = "Proportion of high-degree nodes assigned to defectors",
    y = "Propoption of ever-isolated individuals") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  coord_cartesian(ylim=c(0,0.10))
g.netIntLowDegree

plot(df.netIntLowDegree$assortativityInitial, df.netIntLowDegree$percentIsolation)

summary(lm(percentIsolation ~ assortativityInitial, data=df.netIntLowDegree))
## 
## Call:
## lm(formula = percentIsolation ~ assortativityInitial, data = df.netIntLowDegree)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.04686 -0.04309 -0.04013  0.01659  0.31196 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           0.0423202  0.0008636  49.007   <2e-16 ***
## assortativityInitial -0.0096250  0.0058811  -1.637    0.102    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.05431 on 5486 degrees of freedom
##   (12 observations deleted due to missingness)
## Multiple R-squared:  0.000488,   Adjusted R-squared:  0.0003058 
## F-statistic: 2.678 on 1 and 5486 DF,  p-value: 0.1018

Several explanations

Evolution of cooperation

This seems to occur regardless of initial defector/cooperator position (the same number of C's become D's, and the same number of D's become C's)